using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Hosting;
using System.Web.UI.WebControls;

namespace CafeF.Mobile.Common
{
    public class MailUtils
    {
        public static bool SendMail(string nameTo, string emailTo, string subject, string body)
        {
            string email = ConfigurationManager.AppSettings["EmailAdmin"].ToString();
            return SendMail(email, email, nameTo, emailTo, subject, body);
        }
        public static bool SendMail(string nameFrom, string emailFrom, string nameTo, string emailTo, string subject, string body)
        {
            try
            {
                SmtpClient smtpClient = new SmtpClient();
                MailMessage msg = new MailMessage();
                MailAddress fromAddress = new MailAddress(emailFrom, nameFrom);

                msg.To.Add(new MailAddress(emailTo, nameTo));
                msg.From = fromAddress;
                msg.Subject = subject;
                msg.Body = body;
                msg.BodyEncoding = System.Text.Encoding.UTF8;
                msg.IsBodyHtml = true;
                msg.Priority = MailPriority.Normal;

                if ((ConfigurationManager.AppSettings["MailHostType"] ?? "") == "Gmail")
                {
                    //Add the Creddentials- use your own email id and password
                    smtpClient.Credentials = new System.Net.NetworkCredential((ConfigurationManager.AppSettings["MailAccount"] ?? ""), (ConfigurationManager.AppSettings["MailPassword"] ?? ""));
                    //smtpClient.Port = 587 'Gmail works on this port
                    //smtpClient.Host = "smtp.gmail.com"
                    smtpClient.EnableSsl = true;
                    //Gmail works on Server Secured Layer
                }
                smtpClient.Host = ConfigurationManager.AppSettings["MailHost"] ?? "";
                smtpClient.Port = int.Parse(ConfigurationManager.AppSettings["MailPort"] ?? "0");
                smtpClient.Send(msg);
            }
            catch (Exception ex)
            {
                //Throw New Exception(ex.ToString)
                return false;
            }
            return true;
        }
        public static string LoadTemplateFile(MailType type, string[] param)
        {
            try
            {
                using (System.IO.StreamReader reader = System.IO.File.OpenText(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["MailTemplate." + Enum.GetName(typeof(MailType), type)] ?? "")))
                {
                    string content = reader.ReadToEnd();
                    reader.Close();
                    for (int i = 0; i < param.Length; i++)
                    {
                        content = content.Replace("{{" + i + "}}", param[i]);
                    }
                    return content;
                }
            }
            catch (Exception ex)
            {
                return "";
            }
        }
    }
    public enum MailType
    {
        Registration, ForgotPassword
    }
}
